package com.heima.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;

import static com.heima.common.constants.ArticleConstants.*;

import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Resource
    private ApArticleMapper apArticleMapper;
    // 单页最大加载的数字
    private final static int MAX_PAGE_SIZE = 50;
    // 单页最小加载的数字
    private final static int MIN_PAGE_SIZE = 10;

    @Override
    public ResponseResult<List<ApArticle>> load(Short loadType, ArticleHomeDto dto) {
        //分页条数限定
        Integer size = dto.getSize();
        size = Math.min(size, MAX_PAGE_SIZE);
        size = Math.max(size, MIN_PAGE_SIZE);
        dto.setSize(size);

        //对与类型的校验
        if (ObjectUtil.notEqual(loadType, LOADTYPE_LOAD_MORE) && ObjectUtil.notEqual(loadType, LOADTYPE_LOAD_NEW)) {
            loadType = LOADTYPE_LOAD_MORE;
        }

        //时间校验
        if (ObjectUtil.isEmpty(dto.getMaxBehotTime())) {
            dto.setMaxBehotTime(new Date());
        }
        if (ObjectUtil.isEmpty(dto.getMinBehotTime())) {
            dto.setMinBehotTime(new Date());
        }

        //查询数据
        List<ApArticle> articleList = this.apArticleMapper.loadArticleList(dto, loadType);
        return ResponseResult.okResult(articleList);
    }

    @Resource
    private ApArticleConfigMapper apArticleConfigMapper;
    @Resource
    private ApArticleContentMapper apArticleContentMapper;

    @Override
    public ResponseResult<Long> saveArticle(ArticleDto dto) {
        //1.检查参数
        if (ObjectUtil.isEmpty(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle apArticle = BeanUtil.toBean(dto, ApArticle.class);

        //2.判断是否存在id
        if (ObjectUtil.isEmpty(dto.getId())) {
            //2.1 不存在id  保存  文章  文章配置  文章内容
            //保存文章
            super.save(apArticle);

            //保存配置
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);

            //保存文章内容
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);
        } else {
            //2.2 存在id   修改  文章  文章内容

            //修改  文章
            super.updateById(apArticle);

            //修改文章内容
            LambdaQueryWrapper<ApArticleContent> queryWrapper = Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getId());
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(queryWrapper);
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }

        //3.结果返回 文章的id
        return ResponseResult.okResult(apArticle.getId());
    }
}
